--每日面板答题
--#include "data\config\activityconfig\DailyActivity\PanelAnswerConfig.lua" once


--[[
面板答题正在进行中
]]
function InPanelAnswerDoing()
	local dVar = System.getDyanmicVar()
	if dVar.panelQuestionIdx then
		return true
	end
	return false
end

--[[
面板答题开始
]]
function PanelAnswerStart( )
	local dVar = System.getDyanmicVar()
	dVar.panelQuestionIdx = 0			--活动开启时间

	if not DrawPanelAnswerQuestionFromBank( PanelAnswerConfig.answerNum ) then
		print("DrawPanelAnswerQuestion error !")
		return
	end

	local NpcPtr = System.getGlobalNpc()
	Fuben.unregScriptCallback(NpcPtr, "LoopPanelAnswerSendQuestion")
	Actor.regScriptTimer(NpcPtr, 0, PanelAnswerConfig.beginTime*1000, 0, 1, "LoopPanelAnswerSendQuestion")

	System.broadcastTipmsg( Lang.ScriptTips.PanelAnswer001, ttScreenCenter + ttChatWindow )	
end

--[[
面板答题结束
]]
function PanelAnswerStop( )
	local dVar = System.getDyanmicVar()
	dVar.panelQuestionIdx  = nil			--活动开启时间

	System.broadcastTipmsg( Lang.ScriptTips.PanelAnswer002, ttScreenCenter + ttChatWindow )	
end

--[[
从题库中抽取题目
]]
function DrawPanelAnswerQuestionFromBank( answerNum )
	local dVar = System.getDyanmicVar()
	dVar.panelAnswersIdx = {}
	local panelAnswersIdx = CommonAnswerDrawAnswersIdxInBank( PanelAnswerConfig.answerBankId, answerNum )
	for i, answerIdx in ipairs( panelAnswersIdx or {}) do
		dVar.panelAnswersIdx[i] = answerIdx
		print("DrawPanelAnswerQuestionFromBank idx="..i..", answerIdx="..answerIdx)
	end

	for i=1, answerNum do
		if not dVar.panelAnswersIdx[i] then
			return false
		end
	end
	return true
end

--[[
场景答题发送题目
]]
function LoopPanelAnswerSendQuestion()
	--print("LoopPanelAnswerSendQuestion...")
	local newQestionIdx = GetPanelAnswerCurQuestionIdx() + 1 			--下一题开始
	SetPanelAnswerCurQuestionIdx( newQestionIdx )
	print("LoopPanelAnswerSendQuestion newQestionIdx="..newQestionIdx)

	local question = GetPanelAnswerCurQuestion()
	if not question then
		print("LoopPanelAnswerSendQuestion, no question")
		PanelAnswerStop()
		return
	end

	local activityId 	= PANEL_ANSWER_ACTIVITY
	local answerArea 	= nil
	local lastTime 		= PanelAnswerConfig.answerCd
	local levelLimit 	= PanelAnswerConfig.levelLimit
	local score 		= 5
	BroadCommonAnswerQuestionInArea( activityId, PanelAnswerConfig.sceneId, answerArea, lastTime, newQestionIdx, question, score, enterLevelLimit )

	local NpcPtr = System.getGlobalNpc()
	Fuben.unregScriptCallback(NpcPtr, "LoopPanelAnswerCheck")
	Actor.regScriptTimer(NpcPtr, 0, PanelAnswerConfig.answerCd*1000, 0, 1, "LoopPanelAnswerCheck")
end

--[[
答题完毕，到发送下一题题目的时间
]]
function LoopPanelAnswerCheck( )
	local questionIdx = GetPanelAnswerCurQuestionIdx()
	if questionIdx <= 0 then
		print("LoopPanelAnswerCheck, panel answert over")
		return
	end
	print("LoopPanelAnswerCheck, questionIdx="..questionIdx)
	CheckPanelAnswer( questionIdx )			--检查答案

	local NpcPtr = System.getGlobalNpc()
	Fuben.unregScriptCallback(NpcPtr, "LoopPanelAnswerSendQuestion")
	Actor.regScriptTimer2S(NpcPtr, 0, PanelAnswerConfig.waitCd*1000, 0, 1, "LoopPanelAnswerSendQuestion")
end

function GetPanelAnswerCurQuestionIdx()
	local dVar = System.getDyanmicVar()
	return dVar.panelQuestionIdx or 0
end

function SetPanelAnswerCurQuestionIdx( newQuestionIdx )
	local dVar = System.getDyanmicVar()
	dVar.panelQuestionIdx = newQuestionIdx
end

--[[
获取当前题目
]]
function GetPanelAnswerCurQuestion()
	local dVar = System.getDyanmicVar()
	local questionIdx = GetPanelAnswerCurQuestionIdx()
	local idx = dVar.panelAnswersIdx[questionIdx]
	if not idx then
		return nil
	end
	return  GetCommonAnswerQuestionByIdx( PanelAnswerConfig.answerBankId, idx )
end


--[[
判断玩家是否答题正确
questionIdx：第N轮答题
]]
function CheckPanelAnswer( questionIdx )
	--print("AnswerCommon.JudgeAnswerFree() now="..System.getCurrMiniTime())
	local question = GetPanelAnswerCurQuestion()
	if not question then
		print("CheckPanelAnswer, no question")
		return
	end

	local playerList = LuaHelp.getSceneActorListById( PanelAnswerConfig.sceneId )
	for k, player in ipairs( playerList or {} ) do
		CheckPanelAnswerActorAnswer( player, PANEL_ANSERT_ACTIVITY, questionIdx, question )
	end
end

--[[
获取玩家选择的答案
]]
function GetPanelAnswerActorAnswer( sysarg )
	return 0
end

function CheckPanelAnswerActorAnswer( sysarg, activityId, questionIdx, question )
	local actorAnswerIdx = GetPanelAnswerActorAnswer( sysarg )					--根据玩家站立的区域，判断答案
	local rightAnswerIdx = GetCommonAnswerRightAnswerIdx( question )
	local addScore = 5
	SendCommonAnswerCheckResult( sysarg, activityId, questionIdx, actorAnswerIdx, rightAnswerIdx, addScore )
end
